﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;


namespace EncryptionServer
{
    public partial class FrmPort : Form
    {
        public RefreshSetHander portRefreshHander;
        public FrmPort()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (CheckPort())
            {
                if (PortEnable())
                {
                    Properties.Settings.Default.deciperServerPort =Convert.ToInt32(txtDeciperPort.Text);
                    Properties.Settings.Default.OrderCRCServerPort = Convert.ToInt32(txtCrcPort.Text);
                    Properties.Settings.Default.Save();
                    //SocketMethod.TcpPort = Convert.ToInt32(txtPort.Text);
                    Invoke(portRefreshHander);
                    MessageBox.Show("设置成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                else
                {
                    MessageBox.Show("该端口已占用", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
            }
        }
        private bool CheckPort()
        {
            bool flag = false;
            if (string.IsNullOrEmpty(txtDeciperPort.Text.Trim()))
            {
                MessageBox.Show("请输入安全端口号","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtDeciperPort.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtCrcPort.Text.Trim()))
            {
                MessageBox.Show("请输入Crc端口号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtCrcPort.Focus();
                return false;
            }
            int Deciperport = Convert.ToInt32(txtDeciperPort.Text);
            int CrcPort =Convert.ToInt32(txtCrcPort.Text);

            if (Deciperport >= 1024 && Deciperport <= 65535 && CrcPort >= 1024 && CrcPort <= 65535)
            {
                flag = true;
            }
            else
            {
                MessageBox.Show("端口范围必须为：1024-65535","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                flag = false;
            }
            return flag;
        }

        private bool PortEnable()
        {
            TcpListener listener =null;
            try
            {
                listener = new TcpListener(Convert.ToInt32(txtDeciperPort.Text));
                listener.Start();
            }
            catch
            { }
            bool flag = false;
            bool tcpListen=false;
            bool udpListen=false;//设定端口状态标识位
            System.Net.IPAddress myIpAddress =new IPAddress (Dns.GetHostByName(Dns.GetHostName()).AddressList[0].Address);
            System.Net.IPEndPoint myIpEndPoint = new IPEndPoint(myIpAddress, Convert.ToInt32(txtDeciperPort.Text));
            try
            {
                System.Net.Sockets.TcpClient tcpClient=new TcpClient();
                tcpClient.Connect(myIpEndPoint);//对远程计算机的指定端口提出TCP连接请求
                tcpClient.Close();
                tcpListen=true;
            }
            catch{}
            try
            {
                System.Net.Sockets.UdpClient udpClient = new UdpClient();
                udpClient.Connect(myIpEndPoint);//对远程计算机的指定端口提出UDP连接请求
                udpClient.Close();
                udpListen = true;
            }
            catch { }
            finally
            {
                listener.Stop();
            }
            if (tcpListen == false && udpListen == false)
            {
                flag = false;
            }
            else
            {
                flag = true;
            }

            return flag;
        }

        private void FrmPort_Load(object sender, EventArgs e)
        {
            txtDeciperPort.Text =  Properties.Settings.Default.deciperServerPort.ToString();
            txtCrcPort.Text = Properties.Settings.Default.OrderCRCServerPort.ToString();
        }
        
    }
}
